package cn.xenosp.server.web.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.tools.CronUtil;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.scheduler.entity.ScheduledTask;
import cn.xenosp.server.module.scheduler.entity.ScheduledTaskLog;
import cn.xenosp.server.module.scheduler.entity.ScheduledTaskVo;
import cn.xenosp.server.module.scheduler.logs.TaskLogService;
import cn.xenosp.server.module.scheduler.service.DynamicTaskScheduler;
import cn.xenosp.server.module.scheduler.service.TaskStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2025-11-25 13:38
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/scheduled/")
public class ScheduledTaskController {

    @Autowired
    private TaskStorageService taskStorageService;
    @Autowired
    private TaskLogService taskLogService;

    @PostMapping("/getCron")
    public Result<?> getCron(@RequestBody String body){
        try {
            JSONObject entries = JSONUtil.parseObj(body);
            return Result.OK("", CronUtil.getCron(entries.getStr("cron"),entries.getInt("next")));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @PostMapping("/task/getPage")
    public Result<?> getTypePage(@RequestBody String body){
        try {
            ScheduledTaskVo scheduledTask = ObjectConvert.urlStringToClass(body, ScheduledTaskVo.class);
            return Result.OK("",taskStorageService.getPage(scheduledTask));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @PostMapping("/task/add")
    public Result<?> addTask(@RequestBody ScheduledTask scheduledTask){
        try {
            taskStorageService.addTask(scheduledTask);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @PostMapping("/task/save")
    public Result<?> saveTask(@RequestBody ScheduledTask scheduledTask){
        try {
//            ScheduledTask scheduledTask = ObjectConvert.urlStringToClass(body, ScheduledTask.class);
            taskStorageService.updateTask(scheduledTask);
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @PostMapping("/task/del")
    public Result<?> delTask(@RequestBody ScheduledTask scheduledTask){
        try {
//            ScheduledTask scheduledTask = ObjectConvert.urlStringToClass(body, ScheduledTask.class);
            taskStorageService.deleteTask(scheduledTask.getId());
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @PostMapping("/task/execute")
    public Result<?> execute(@RequestBody String body){
        try {
            JSONObject entries = JSONUtil.parseObj(body);
            taskStorageService.execute(entries.getStr("execute"),entries.getStr("id"));
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @PostMapping("/task/logs")
    public Result<?> logs(@RequestBody ScheduledTask scheduledTask){
        try {
//            ScheduledTask scheduledTask = ObjectConvert.urlStringToClass(body, ScheduledTask.class);
            List<ScheduledTaskLog> logs = taskLogService.getLogsByTaskId(scheduledTask.getId());
            return Result.OK("",logs);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @PostMapping("/task/log")
    public Result<?> log(@RequestBody ScheduledTask scheduledTask){
        try {
            List<ScheduledTaskLog> logs = taskLogService.getLogsByTaskId(scheduledTask.getId());
            return Result.OK("",logs.size()>0?logs.get(0):null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }

    @PostMapping("/task/countStatus")
    public Result<?> countStatus(@RequestBody ScheduledTask scheduledTask){
        try {
            return Result.OK("",taskStorageService.countStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
}
